package com.example.essayRes;

import java.util.Date;
import java.text.SimpleDateFormat;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Essay {
	private String ID, title, summary, source, body, author, submitDate, changeDate;
	int status;

	public Essay() {
		title = summary = source = body = author = submitDate = changeDate = null;
		status=Essay.ESSAY_STATUS_DRAFT;
		SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		submitDate= timeFormat.format(new Date());
	}
	public Essay(String newID, String newTitle, String newSummary, String newSource, String newbody, String newAuthor,int newStatus) {
		ID = newID;
		title = newTitle;
		summary = newSummary;
		source = newSource;
		body = newbody;
		author = newAuthor;
		status = newStatus;
		SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		submitDate= timeFormat.format(new Date());
		changeDate = timeFormat.format(new Date());
	}
	public void setChangeDate(){
		SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		changeDate=timeFormat.format(new Date());
	}
	//String setUpTime=null;
	//boolean releaseState=false;
	//boolean checkState=false;
	public void setTitle(String newTitle){
		title=newTitle;
	}
	public void setSummary(String newSummary){
		summary=newSummary;
	}
	public void setAuthor(String newAuthor){
		author=newAuthor;	
	}
	public void setSource(String newSource){
		source=newSource;
	}
	public void setBody(String newBody){
		body=newBody;
	}
	public void setStatus(int newStatus){
		status=newStatus;
	}
	public String getID() {
		return ID;
	}
	public String getTitle (){
		return title;
	}
	public String getSummary (){
		return summary;
	}
	public String getAuthor (){
		return author;
	}
	public String getSource (){
		return source;
	}
	public String getBody (){
		return body;
	}
	public String getSubmitDate(){
		return submitDate;
	}
	public String getChangeDate(){
		return changeDate;
	}
	public int getStatus(){
		return status;
	}
	public static Essay xmlToEssay(Element eleEssay) {
		Essay rtn = new Essay();
		NodeList recNode = eleEssay.getChildNodes();
		if (recNode.getLength() > 0 && recNode.item(0).getNodeName().equals("record")) {
			NodeList tagList = recNode.item(0).getChildNodes();
			for (int k = 0; k < tagList.getLength(); k++) {
				String tagName = tagList.item(k).getNodeName(), tagValue = null;
				if (tagList.item(k).getFirstChild() != null)
					tagValue = tagList.item(k).getFirstChild().getNodeValue();
				//System.out.println(tagName + " : " + tagValue);
				switch (tagName) {
					case "primarykey":
						rtn.ID = tagValue;
						break;
					case "title":
						rtn.title = tagValue;
						break;
					case "summary":
						rtn.summary = tagValue;
						break;
					case "source":
						rtn.source = tagValue;
						break;
					case "body":
						rtn.body = tagValue;
						break;
					case "status":
						rtn.status = Integer.parseInt(tagValue);
						break;
					case "author":
						rtn.author = tagValue;
						break;
					case "submitDate":
						rtn.submitDate = tagValue;
						break;
					case "changeDate":
						rtn.changeDate = tagValue;
						break;
				}
			}
		}
		return rtn;
	}
	
	public static String getStatusOfSubmmitedComment(int essStatus) {
		switch (essStatus) {
		case ESSAY_STATUS_EXAMINEFIRST:
			return COMMENT_STATUS_SUBMITTEDFIRST;
		case ESSAY_STATUS_EXAMINEMID:
			return COMMENT_STATUS_SUBMITTEDMID;
		case ESSAY_STATUS_EXAMINEFINAL:
			return COMMENT_STATUS_SUBMITTEDFINAL;
		case ESSAY_STATUS_EXAMINEULTI:
			return COMMENT_STATUS_SUBMITTEDULTI;
		default:
			return null;
		}
	}
	
	public static int nextStatus(int status) {
		if (status != ESSAY_STATUS_EXAMINEULTI)
			return status + 1;
		else
			return status;
	}
	
	public static void print(Essay essay) {
		System.out.println("ID " + essay.getID());
		System.out.println("title " + essay.getTitle());
		System.out.println("summary " + essay.getSummary());
		System.out.println("source " + essay.getSource());
		System.out.println("body " + essay.getBody());
		System.out.println("author " + essay.getAuthor());
		System.out.println("status " + essay.getStatus());
		System.out.println("submitDate " + essay.getSubmitDate());
		System.out.println("changeDate " + essay.getChangeDate());
		
	}
	
	@Override
    public boolean equals(Object obj) {
		if (!(obj instanceof Essay)) return false;
		Essay ess = (Essay) obj;
		return ess.getID().equals(this.ID); 
    }
	
	public static final String COMMENT_STATUS_SUBMITTEDFIRST ="fisrt";
	public static final String COMMENT_STATUS_SUBMITTEDMID ="mid";
	public static final String COMMENT_STATUS_SUBMITTEDFINAL ="final";
	public static final String COMMENT_STATUS_SUBMITTEDULTI ="ultimate";
	public static final String COMMENT_STATUS_EXAMINING ="examining";
	public static final String COMMENT_STATUS_AUTHOR ="author";
	public static final int ESSAY_STATUS_DRAFT=0;
	public static final int ESSAY_STATUS_EXAMINEFIRST=1;
	public static final int ESSAY_STATUS_EXAMINEMID=2;
	public static final int ESSAY_STATUS_EXAMINEFINAL=3;
	public static final int ESSAY_STATUS_EXAMINEULTI=4;
	public static final int ESSAY_STATUS_RELEASED=5;
}